import math
from math import *

import pyglet
from pyglet.window import key, mouse
from pyglet.gl import *
from pyglet import gl

import cocos
from cocos.actions import *
from cocos.layer import *
from cocos.scene import *
from cocos.sprite import *
from cocos.director import director
from cocos.scenes.transitions import *
from cocos.euclid import Point2
from cocos.draw import *
from cocos.text import *

import Box2D as box2d

from globalvars import *


def point_float(x,y):
    return float(x), float(y)

class SingleQuad(cocos.cocosnode.CocosNode):
    def __init__(self,p1,p2,p3,p4, color=(0,0,0,255)):
        cocos.cocosnode.CocosNode.__init__(self)
        #ensure float coords
        self.vertexes = [point_float(*p1),point_float(*p2),point_float(*p3),point_float(*p4)]
        if color is None:
            color = (255,255,255,255)
        elif len(color)==3:
            color = tuple(list(color).append(255))
        self.color = color

    def draw(self):
        glPushMatrix()
        self.transform()
        glColor4ub( *self.color )
        glLineWidth(1)
        glBegin(GL_QUADS)
        for v in self.vertexes:
            glVertex2f(*v)
        glEnd()
        glPopMatrix()
        
class SingleLine(cocos.cocosnode.CocosNode):
    def __init__(self, p1,p2, color = None):
        cocos.cocosnode.CocosNode.__init__(self)
        self.vertexes = [point_float(*p1),point_float(*p2)]
        if color is None:
            color = (0,0,0,255)
        elif len(color)==3:
            color = tuple(list(color).append(255))
        self.color = color

    def draw(self):
        glPushMatrix()
        self.transform()
        glBegin(GL_LINES)
        glColor4ub( *self.color )
        for v in self.vertexes:
            glVertex2f(*v)
        glEnd()
        glPopMatrix()

class PointBag(cocos.cocosnode.CocosNode):
    def __init__(self, pointsize=4, color=None):
        super(PointBag,self).__init__()
        self.pointsize = pointsize
        if color is None:
            color = (255,255,255,255)
        elif len(color)==3:
            color = tuple(list(color).append(255))
        self.color = color
        self.vertexes = []

    def add_xy(self, x,y):
        self.vertexes.append(point_float(x,y))

    def extend_points(self, points):
        points = [point_float(*p) for p in points]
        self.vertexes.extend(points)

    def set_state(self):
        glPointSize(self.pointsize)
        glColor4ub(*self.color)

    def draw(self):
        glPushMatrix()
        self.transform()
        self.set_state()
        glBegin(GL_POINTS)
        for p in self.vertexes:
            glVertex2f(*p)
        glEnd()
